const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company:[],
    longUID:'',
    headernav:[{name:'关于公司',navid:0,img:'../../images/shangpu/gywm.jpg'},{name:'供求信息',navid:1,img:'../../images/shangpu/gqxx.jpg'},{name:'小视频',navid:2,img:'../../images/shangpu/xiaoshipin.png'}],
    dianColor:['#149ce7','#fe8117','#f5305e'],    
    navindex:0,
    LoginUID:0,
    ggjjname:'展开',
    ggjjicon:' icon-xiangxia',
    gsjjheight:'150rpx',
    lxfsname:'收起',
    lxfsicon:' icon-xiangshang',
    lxfsheight:'auto',
    LxBgdh:'',
    LxEmail:'',
    LxGsdz:'',
    LxLxdh:'',
    lxfsborder:'',
    IsLxfs:1,
    nolxfsimg:'../../images/shangpu/nolxfs.jpg',
    LoginUserRankName:'',
    AllLookNum:'',
    HaveLookNum:'',
    NextRankName:'',
    showModal:false,
    gqlisttype:1,
    gqlistnav:[{name:'供应信息',shid:1},{name:'求购信息',shid:2}],
    intCurrPage:1,
    intPageSize:15,
    hasMore:true,
    gqlist:[],
    nopic:'../../images/zcczNopic.png',
    splist:[],
    indexCurrent:0,
    is_play:false,
    vodurl:'',
  },
  // 改变供求信息里的选项卡
  gqlistnavTab:function (e) {
    if (this.data.gqlisttype != e.currentTarget.dataset.shid) {
      this.setData({
        gqlisttype:e.currentTarget.dataset.shid
      })
      this.getGqListTop()
    }
  },
  // 查看联系方式
  ckLxfs:function () {
    if (app.checkFjLogin()) {
        var FjLoginLog = wx.getStorageSync('FjLoginLog');           
        this.setData({
          LoginUID:FjLoginLog.UID
        })        
        var that = this;
        app.getDaoPost('api/FeijiuApp/Feijiu_GetShopLxfs_Jm', {
          strUID: this.data.longUID,
          strLoginUID:FjLoginLog.UID,
          strLoginUpw:FjLoginLog.UPwd,
          strRSA:''
        }, r => {
          if (r.code == 200) {
            var result = JSON.parse(app.aesDec(r.data.GqInfo));           
            if (result.ErrNum == 111) {
              that.setData({
                LoginUserRankName:result.LoginUserRankName,
                AllLookNum:result.AllLookNum,
                HaveLookNum:result.HaveLookNum,
                NextRankName:result.NextRankName,
                showModal:true
              })
            }else if (result.ErrNum == 11) {
              that.setData({
                IsLxfs:1
              })
            }else {
              wx.showModal({
                title: 'Fiejiu网提示您',
                content: '该信息已经被别人保护',
                showCancel: false,
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F'
              })              
            }
          }
        })


return


        var url = 'api/FeijiuApp/GetShopLxfs_Jm?strUID='+this.data.longUID+'&strLoginUID='+FjLoginLog.UID+'&strLoginUpw='+FjLoginLog.UPwd+'&strRSA=';
        app.getDaoGet(url,function (r) {
          if (r.code == 200) {
            var result = JSON.parse(app.aesDec(r.data));           
            if (result.ErrNum == 111) {
              that.setData({
                LoginUserRankName:result.LoginUserRankName,
                AllLookNum:result.AllLookNum,
                HaveLookNum:result.HaveLookNum,
                NextRankName:result.NextRankName,
                showModal:true
              })
            }else if (result.ErrNum == 11) {
              that.setData({
                IsLxfs:1
              })
            }else {
              wx.showModal({
                title: 'Fiejiu网提示您',
                content: '该信息已经被别人保护',
                showCancel: false,
                cancelColor: '#000000',
                confirmText: '确定',
                confirmColor: '#3CC51F'
              })              
            }
          }
        })


    }else{
       wx.navigateTo({ url: '../../pages/login/index' }); 
    }    
  },
  //确认查看
  onConfirm:function () {
    this.setData({
      showModal:false
    });
    var that = this
    var FjLoginLog = wx.getStorageSync('FjLoginLog')    
    var url = 'api/FeijiuApp/GetShopLxfsInfo_Jm?strUID='+this.data.longUID+'&strLoginUID='+FjLoginLog.UID+'&strLoginUpw='+FjLoginLog.UPwd+'&strRSA=';
    app.getDaoGet(url,function (r) {
      var result = JSON.parse(app.aesDec(r.data));
      if (result.ErrNum == 11 || result.ErrNum == 1 ) {
        that.setData({
          IsLxfs:1
        })        
      }else{
        wx.showModal({
          title: 'Fiejiu网提示您',
          content: '您现在无权查看,有问题可以联系客服人员',
          showCancel: false,
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F'
        })
      }
    })
  },
  // 公司简介收起展示
  gsjjzk:function () {
    if (this.data.ggjjicon == ' icon-xiangxia') {
      this.setData({
        ggjjname:'收起',
        ggjjicon:' icon-xiangshang',
        gsjjheight:'auto'
      })
    }else{
      this.setData({
        ggjjname:'展开',
        ggjjicon:' icon-xiangxia',
        gsjjheight:'150rpx'
      })
    }
  },
  // 联系方式收起展示
  lxfszk:function () {
    if (this.data.lxfsicon == ' icon-xiangxia') {
      this.setData({
        lxfsname:'收起',
        lxfsicon:' icon-xiangshang',
        lxfsheight:'auto',
        lxfsborder:''
      })
    }else{
      this.setData({
        lxfsname:'展开',
        lxfsicon:' icon-xiangxia',
        lxfsheight:'0rpx',
        lxfsborder:'border-bottom: 0'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      // longUID:1541382
      longUID:options.longUID
      // longUID:'1541382'
    })
    console.log(options)

    var url = 'api/FeijiuApp/GetShopAbout?longUID='+this.data.longUID+'&LoginUID='+this.data.LoginUID+'&strRSA=';    
    app.getDaoGet(url,function (r) {
      console.log(r)
      if (r.code == 200) {
        wx.setNavigationBarTitle({
          title: r.data.ComName 
        })
        r.data.HyYear = that.tmpnewchar(r.data.HyYear);

        if (r.data.isDeBao == 1) {
          r.data.isDeBaoname = '已认证'
        }else{
          r.data.isDeBaoname = '未认证'
        }
        that.setData({
          LxBgdh : app.aesDec(r.data.LxBgdh),
          LxEmail : app.aesDec(r.data.LxEmail),
          LxGsdz : app.aesDec(r.data.LxGsdz),
          LxLxdh : app.aesDec(r.data.LxLxdh),            
        })

        that.setData({
          company:r.data,
          IsLxfs:r.data.IsLxfs
        })
      }      
    })
  
  },
  navTab:function (e) {
    this.setData({
      navindex:e.currentTarget.dataset.shid
    })
    if (e.currentTarget.dataset.shid == 1) {
      this.getGqListTop()
    }
    if (e.currentTarget.dataset.shid == 2) {
      this.getXspList()
    }    
  },

  getXspList:function () {    
    var that = this;
    //LoginUID：登录用户ID，Type：1小视频 2宣传片，shid：-1全部 0未审核 1审核通过  2未通过 ，intCurrPage：当前页数     
    var api_input = {
      LoginUID:this.data.longUID,
      Type:1,
      shid:1,
      intCurrPage:1
    }
    app.getDaoPost('api/FeijiuApp/Feijiu_Get_UserVideoList',api_input,function (r) {
      console.log(r)
      if (r.code == 200) {
        if (r.data.VideoAuth == 1) {
          var splist = r.data.data.ds;
          // var thisarr = [];
          for (var i = 0; i < splist.length; i++) {
            splist[i].VideoDuration = that.formatTime(splist[i].VideoDuration);
            // if (splist[i].shid == that.data.spshid) {
            //   thisarr.push(splist[i]);
            // }
          }
          that.setData({
            splist:splist
          })
        }
      }
    })
  },
  formatTime:function (seconds){
      let hour = Math.floor(seconds / 3600) >= 10 ? Math.floor(seconds / 3600) : '0' + Math.floor(seconds / 3600);
      seconds -= 3600 * hour;
      let min = Math.floor(seconds / 60) >= 10 ? Math.floor(seconds / 60) : '0' + Math.floor(seconds / 60);
      seconds -= 60 * min;
      let sec = seconds >= 10 ? seconds : '0' + seconds;
      return min + ':' + sec;
  },  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.navindex == 1) {
      this.setData({
        hasMore:true,
        gqlist:[],
        intCurrPage:1
      }) 
      this.getGqList();
      wx.stopPullDownRefresh() 
    }else{
      wx.stopPullDownRefresh()  
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasMore && this.data.navindex == 1) {
      this.getGqList(); 
    }  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button'){
      console.log(res)
      var idx = res.target.dataset.idx;
  　　var shareObj ={
  　　　　title: this.data.splist[idx].Title,
  　　　　path: '/pages/gongqiuxinxi/shangpu',
  　　　　imageUrl: this.data.splist[idx].VideoPic,
  　　}
      var api_input = {
        ID:this.data.splist[idx].id
      }
      app.getDaoPost('api/FeijiuApp/Feijiu_Add_UserVideoSharesNum',api_input,function (r) {

      }) 
  　　return shareObj;
    }
  },
  // 把数字1转成一
  tmpnewchar:function (num) {    
    var tmpnewchar = '一';
    switch (num) {     
      case "1": tmpnewchar = "一" ; break;
      case "2": tmpnewchar = "二" ; break;
      case "3": tmpnewchar = "三" ; break;
      case "4": tmpnewchar = "四" ; break;
      case "5": tmpnewchar = "五" ; break;
      case "6": tmpnewchar = "六" ; break;
      case "7": tmpnewchar = "七" ; break;
      case "8": tmpnewchar = "八" ; break;
      case "9": tmpnewchar = "九" ; break;
      case "10": tmpnewchar = "十" ; break;
      case "11": tmpnewchar = "十一" ; break;
      case "12": tmpnewchar = "十二" ; break;
      case "13": tmpnewchar = "十三" ; break;
      case "14": tmpnewchar = "十四" ; break;
      case "15": tmpnewchar = "十五" ; break;
      case "16": tmpnewchar = "十六" ; break;
      case "17": tmpnewchar = "十七" ; break;
      case "18": tmpnewchar = "十八" ; break;
      case "19": tmpnewchar = "十九" ; break;
    }
    return tmpnewchar;
  },
  // 弹出框取消
  onCancel:function () {
    this.setData({
      showModal:false
    });    
  },
  //获取供求列表
  getGqList:function () {
    var that = this;
    var url = 'api/FeijiuApp/GetShopGqList?longUID='+this.data.longUID+'&type='+this.data.gqlisttype+'&intCurrPage='+this.data.intCurrPage+'&intPageSize='+this.data.intPageSize+'&strRSA=';
    app.getDaoGet(url,function (r) {
      if (r.code == 200) {
        for (var i = 0; i < r.data.List.length; i++) {

          var photoss = app.photoSplitOne(r.data.List[i].photo);

          if (photoss == '') {
             r.data.List[i].photoOne = that.data.nopic;
          }else{
             r.data.List[i].photoOne = photoss;
          }
        }
        var words = that.data.gqlist.concat(r.data.List);
        if (r.data.List.length < that.data.intPageSize) {
          that.setData({
            gqlist:words,
            hasMore:false
          })
        }else{
          that.setData({
            gqlist:words,
            intCurrPage:that.data.intCurrPage+1            
          })          
        }
      }else{
        that.setData({
          hasMore:false
        })
      }
    })
  },
  // 重新获取回到头部页数为1
  getGqListTop:function () {
    this.setData({
      hasMore:true,
      gqlist:[],
      intCurrPage:1
    }) 
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 10
    })
    this.getGqList();   
  },
  videoPlay:function (e) {
    console.log(e)
    var idx = e.currentTarget.dataset.idx;
    var api_input = {
      ID:this.data.splist[idx].id
    }
    var that = this;
    app.getDaoPost('api/FeijiuApp/Feijiu_Add_UserVideoViews',api_input,function (r) {

    })    
    this.setData({
      is_play:true,
      vodurl:this.data.splist[idx].Video
    })
    this.videoContext = wx.createVideoContext('myVideo');
  },
  guanbi_sp:function () {
    this.setData({
      is_play:false,
    })
  }

})